/**
    @author: ASUS
    @date: 2021/2/21
    @Describe:
**/
package kernel

import (
	"fmt"
	influxdb "logprocess/global/db"
	"strings"
	"time"
)

type Writer interface {
	Writes(wc chan *Message)
}

type WriteToDB struct {
	InfluxDBDsn string // influx data source
}

func (w *WriteToDB) Writes(wc chan *Message) {

	infSli := strings.Split(w.InfluxDBDsn, "@")
	client := influxdb.InitClient(infSli[0], infSli[1], infSli[2], 5)

	for v := range wc {
		fmt.Println(v)
		tags := map[string]string{"Path": v.Path, "Method": v.Method, "Scheme": v.Scheme, "Status": v.Status}
		fields := map[string]interface{}{
			"UpstreamTime": v.UpstreamTime,
			"RequestTime":  v.RequestTime,
			"BytesSent":    v.BytesSent,
		}

		influxdb.WritesPoints(client, infSli[3], "nginx_log", tags, fields, time.Now())
	}
}
